﻿using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TSJDWB13
    {
        public TSJDWB13()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_STOCK();
            this.DataContext = MV;
            InitializeComponent(); 
            LO = "01";
            LB = "ZZ";
            VAR = "0700";
            DESS = "0700";
            DTSTART = Helpers.Helper.GetPreviousday(-1);
            DTEND = MV.date;
        }
        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.bgWorker_RunWorkerCompleted(sender, e);
            vwMaster.View.MoveCurrentTo(-1);
            EventDispatcher.Instance.RaiseStatus("完成数据更新", StatusEventType.StopProgress);
        }

        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            base.bgWorker_DoWork(sender, e);
            EventDispatcher.Instance.RaiseStatus("开始更新数据", StatusEventType.RunningProgress);
            TMV.EXEWI09(PM["DTSTART"], "F");
            TMV.EXEWI09(PM["DTEND"], "F");

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            if(dwMain==null)
            { }
            if(dwMaster==null)
            {
                KEY = "";
            }
            else
            {
                KEY = dwMaster["WBFRWKRS"].ToString() + dwMaster["WBFRWLFD"].ToString() 
                    + dwMaster["WBFRWPOS"].ToString() + dwMaster["WBFVERSION"].ToString();
            }
            //base.vwMaster_CurrentChanged(sender, e);
        }
        public void FilterMaster_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string filter = "ART LIKE '%" + ART.Trim() + "%'";
                cvMaster.CustomFilter = filter;
            }
            catch(Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex.Message);
            }
            
        }

        public void FilterMasterClear_Click(object sender, RoutedEventArgs e)
        {

            cvMaster.CustomFilter = null;
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            PM["DTSTART"] = DTSTART;
            PM["DTEND"] = DTEND;
            bgWorker.RunWorkerAsync();
            Tracer.Info("", "已经更新完成");
            base.NewCommand(sender, e);

        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            IsBusy = true; 
            dwData = TMV.WBWI.TBMain.NewRow();
            dwData["WBEWMANDANT"] = app.Mandant;
            dwData["WBEWBUCHDAT"] = DTSTART;
            dwData["WBEWFILLER"] = " AND WBEWBUCHDAT||SUBSTR('00000000'||TO_CHAR(WBEWBUCHUHR),-8)>='" + DTSTART + Helpers.Helper.Right("00000000" + VAR, 4) + "0000" + "' AND WBEWBUCHDAT||SUBSTR('00000000'||TO_CHAR(WBEWBUCHUHR),-8)<='" + DTEND + Helpers.Helper.Right("0000" + DESS, 4) + "0000'";
            string src, str1 = "", str2 = "", str3 = "";
            src = DTSTART;
            Helpers.Helper.GetSplitDate(ref str1, ref str2, ref str3, src);
            string date1 = str1;
            Helpers.Helper.GetSplitDate(ref str1, ref str2, ref str3, DTEND);
            string date2 = str1;
            SResult = TMV.QSJHPROD(dwData); 
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                VAR = Helpers.Helper.Right("0000" + VAR, 4);
                DESS = Helpers.Helper.Right("0000" + DESS, 4); 
                row["TIMERANGE"] = date1+ VAR.Substring(0, 2) + ":" + VAR.Substring(2, 2) + "-" + date2 + DESS.Substring(0, 2) + ":" + DESS.Substring(2, 2);
                if (row["REMARK"].ToString().Trim()=="-")
                {
                    row["REMARK"] = " ";
                }
            }
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
            NormalTask = Task.Factory.StartNew(() => UPDATEDCGQTY(), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
            IsBusy = false;
        }
        public void UPDATEDCGQTY()
        {
            dwData = TMV.WBWI.TBMain.NewRow();
            dwData["WBEWMANDANT"] = app.Mandant;
            dwData["WBEWBUCHDAT"] = DTSTART;
            dwData["WBEWFILLER"] = " AND WBEWBUCHDAT||SUBSTR('00000000'||TO_CHAR(WBEWBUCHUHR),-8)>='" + DTSTART + Helpers.Helper.Right("00000000" + VAR, 4) + "0000" + "' AND WBEWBUCHDAT||SUBSTR('00000000'||TO_CHAR(WBEWBUCHUHR),-8)<='" + DTEND + Helpers.Helper.Right("0000" + DESS, 4) + "0000'";
            string src, str1 = "", str2 = "", str3 = "";
            src = DTSTART;
            Helpers.Helper.GetSplitDate(ref str1, ref str2, ref str3, src);
            string date1 = str1;
            Helpers.Helper.GetSplitDate(ref str1, ref str2, ref str3, DTEND);
            string date2 = str1;
            SResult = TMV.QSJHWVPROD(dwData);
            foreach(DataRow row in SResult.Tables[0].Rows)
            {
                filter = "WBEWSTKNR='" + row["WBEWSTKNR"].ToString() + "' AND WBFRWPOST='" + row["WBFQPKT"].ToString() + "'";
                rows = TBMain.Select(filter);
                if(rows.Length==0)
                {
                    filter = "WBEWSTKNR='" + row["WBEWSTKNR"].ToString() + "'";
                    rows = TBMain.Select(filter);
                    if (rows.Length == 0) continue;
                    dwData = TBMain.Rows.Add();
                    dwData.ItemArray = rows[0].ItemArray;
                    dwData["DCG"] = row["DCG"];
                    dwData["REMARK"] = "";
                    dwData["WBFRWPOST"] = row["WBFQPKT"];
                    dwData["DCGQTY"] = row["DCGQTY"];
                }
                else
                {
                    foreach (DataRow rowdc in rows)
                    {
                        rowdc["DCGQTY"] = row["DCGQTY"];
                    }
                }
               
            }            
        }
        public override void PrintCommand(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            Set.Merge(TBMain);
            cDR.PRINT(Set);
            base.PrintCommand(sender, e);
        } 
    }
}
